package com.tianyao.compose.baseuse.activity.animate

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.Animatable
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.AnimationVector2D
import androidx.compose.animation.core.FastOutLinearInEasing
import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.SnapSpec
import androidx.compose.animation.core.TwoWayConverter
import androidx.compose.animation.core.VectorConverter
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.animateValueAsState
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.keyframes
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.repeatable
import androidx.compose.animation.core.snap
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Notifications
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.rounded.Favorite
import androidx.compose.material3.Button
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.tianyao.compose.baseuse.R
import com.tianyao.compose.baseuse.theme.ComposeProTheme

/**
 * 动画的低级别api的Activity
 */
@OptIn(ExperimentalMaterial3Api::class)
class AnimatedLowGradeApi3Activity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeProTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier
                        .fillMaxSize()
                        .background(Color.White)
                ) {
                    Greeting()
                }
            }
        }
    }


    @Composable
    fun Greeting() {
        //TestAnimateColorAsState()
        //InfinitRepetableDemo()
        //TestAnimatable4()
        SnapAnimationExample()
    }


    @Preview
    @Composable
    fun TestAnimatable() {
        val value by animateFloatAsState(
            targetValue = 1f,
            animationSpec = tween(
                durationMillis = 300,//动画执行时间(ms)
                delayMillis = 50,//可以指定动画的延迟执行
                easing = LinearOutSlowInEasing//衰减曲线动画效果
            ), label = ""
        )
    }

    @Preview
    @Composable
    fun TestAnimatable2() {
        val value by animateFloatAsState(
            targetValue = 1f,
            animationSpec = keyframes {
                durationMillis = 375
                0.0f at 0 with LinearOutSlowInEasing // for 0-15 ms
                0.2f at 15 with FastOutLinearInEasing // for 15-75 ms
                0.4f at 75 // ms
                0.4f at 225 //ms
            }, label = ""
        )
    }

    @Preview
    @Composable
    fun TestAnimatable3() {
        val value by animateFloatAsState(
            targetValue = 1f,
            animationSpec = infiniteRepeatable(
                animation = tween(durationMillis = 300),
                repeatMode = RepeatMode.Reverse
            ),
            label = ""
        )
    }

    @Composable
    private fun SnapAnimationExample() {
        var targetValue by remember { mutableFloatStateOf(0f) }

        //创建动画使用SnapSpec
        val animatedValue by animateValueAsState(
            targetValue = targetValue,
            typeConverter = Float.VectorConverter,
            animationSpec = snap(delayMillis = 1000), label = ""   //延迟1秒执行
        )

        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(16.dp),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            //创建一个按钮修改targetValue的值
            Button(
                onClick = {
                    targetValue = if (targetValue == 0f) 1f else 0f   //点击后修改targetValue的值
                },
                modifier = Modifier.padding(8.dp)
            ) {
                Text("Toggle Animation")
            }

            Spacer(modifier = Modifier.height(16.dp))

            // Display the animated value
            //修改文本
            Text("Animated Value: $animatedValue", style = MaterialTheme.typography.bodyMedium)
        }
    }

    @Preview
    @Composable
    fun InfinitRepetableDemo() {
        val infiniteTransition = rememberInfiniteTransition(label = "")
        val degrees by infiniteTransition.animateFloat(   //定义旋转角度
            initialValue = 0f,
            targetValue = 359f,
            animationSpec = infiniteRepeatable(
                animation = keyframes {
                    durationMillis = 1500
                    0F at 0
                    359F at 1500
                }), label = ""
        )

        Box(
            modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.Center
        ) {
            Text(
                text = "Hello Compose",
                modifier = Modifier.rotate(degrees = degrees)   //使用动画的旋转角度
            )
        }
    }


    data class MySize(val width: Dp, val height: Dp)

    @Composable
    fun MyAnimation(targetSize: MySize) {
        val animSize: MySize by animateValueAsState<MySize, AnimationVector2D>(
            targetSize,
            TwoWayConverter(
                convertToVector = { size: MySize ->
                    //Extract a float value from each of the 'Dp  fields
                    AnimationVector2D(size.width.value, size.height.value)
                },
                convertFromVector = { vector: AnimationVector2D ->
                    MySize(vector.v1.dp, vector.v2.dp)
                }), label = ""
        )
    }

}
